package com.zmx.controller;

import com.zmx.aop.MyLog;
import com.zmx.entity.SysUserExt;

import com.zmx.mapper.SysUserMapper;
import com.zmx.service.SysUserService;
import com.zmx.utils.R;
import com.zmx.utils.RUtils;
import com.zmx.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 2020/10/19
 * 17:28
 * zmx
 */
@RestController
public class SysUserController {
    @Resource
    SysUserService sysUserService;
    @RequestMapping("/sys/login")
    @ResponseBody
    public R login(@RequestBody SysUser sysUser){

        Subject subject = SecurityUtils.getSubject();
        Md5Hash password = new Md5Hash(sysUser.getPassword(), "chichi", 1024);
        System.out.println(password.toString());
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(sysUser.getUsername(), password.toString());
        subject.login(usernamePasswordToken);
        return R.success("成功");
    }
    @RequestMapping("/sys/user/info")
    @ResponseBody
    public RUtils getUserInfo(){
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser) subject.getPrincipal();

        return RUtils.success("成功返回",null ).put("user",sysUser);
    }
    /* 用户管理管理*/
    @RequestMapping("sys/role/list")
    public RUtils getUserPage(int limit,int offset,String search,String order){
        return sysUserService.findUserPage(limit, offset, search, order);
    }

    @RequestMapping("sys/role/save")
    public RUtils saveUser(@RequestBody SysUserExt sysUser){
        return sysUserService.saveUser(sysUser);
    }
    @MyLog(value = "更新操作",description = "菜单更新")
    @RequestMapping("sys/role/info/{id}")
    public RUtils selectOneUser(@PathVariable long id){
        System.out.println(id);
        return sysUserService.selectByUserId(id);
    }
    @RequestMapping("sys/role/update")
    public RUtils updateUser(@RequestBody SysUserExt sysUser){
        return sysUserService.updateUser(sysUser);
    }
    /*假删除 标志字段，0使用 1 删除 而不真正的删除此段*/
    @RequestMapping("sys/role/del")
    public RUtils deleteFake(@RequestBody List<Long> ids){
        System.out.println(ids);
        return sysUserService.delete(ids);
    }
}
